/*
 * Copyright (c) 2008-2013 Haulmont. All rights reserved.
 * Use is subject to license terms, see http://www.cuba-platform.com/license for details.
 */

package com.haulmont.asu.web.ui.defense_day;

import com.haulmont.asu.app.ExtConstants;
import com.haulmont.asu.entity.*;
import com.haulmont.cuba.core.global.Metadata;
import com.haulmont.cuba.gui.WindowManager;
import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.components.actions.CreateAction;
import com.haulmont.cuba.gui.components.actions.EditAction;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.DataSupplier;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.impl.DsListenerAdapter;

import javax.annotation.Nullable;
import javax.inject.Inject;
import java.util.*;

/**
 * Author: Sergey42
 * Date: 30.11.13 15:33
 */
public class DefenseDayEditor extends AbstractEditor<DefenseDay> {
    @Inject
    protected DataSupplier dataSupplier;
    @Inject
    protected Metadata metadata;
    @Inject
    protected Table scheduleTable;

    @Inject
    protected CollectionDatasource<DefenseDayType, UUID> typeDs;
    @Inject
    protected CollectionDatasource<Commission, UUID> commissionDs;

    @Inject
    protected Label controllerLabel;
    @Inject
    protected Label commissionLabel;
    @Inject
    protected LookupField controllerLF;
    @Inject
    protected LookupPickerField commissionLF;
    @Inject
    protected Button createMemberBlank;
    @Inject
    protected Button createTotalBlank;

    @Override
    public void init(Map<String, Object> params) {
        scheduleTable.removeAction(getAction("scheduleTable.create"));
        scheduleTable.addAction(new CreateAction(scheduleTable, WindowManager.OpenType.DIALOG));

        scheduleTable.removeAction(getAction("scheduleTable.edit"));
        scheduleTable.addAction(new EditAction(scheduleTable, WindowManager.OpenType.DIALOG));

        typeDs.addListener(new DsListenerAdapter<DefenseDayType>() {
            @Override
            public void itemChanged(Datasource<DefenseDayType> ds, @Nullable DefenseDayType prevItem, @Nullable DefenseDayType item) {
                if (item != null) {
                    String code = item.getCode();

                    if (ExtConstants.NORM_CONTROL_CODE.equals(code)) {
                        controllerLabel.setVisible(true);
                        controllerLF.setVisible(true);
                        commissionLabel.setVisible(false);
                        commissionLF.setVisible(false);
                    } else {
                        controllerLabel.setVisible(false);
                        controllerLF.setVisible(false);
                        commissionLabel.setVisible(true);
                        commissionLF.setVisible(true);
                    }

                    createMemberBlank.setVisible(ExtConstants.DEFENSE_CODE.equals(item.getCode()));
                    createTotalBlank.setVisible(ExtConstants.DEFENSE_CODE.equals(item.getCode()));
                }
            }
        });

        commissionDs.addListener(new DsListenerAdapter<Commission>() {
            @Override
            public void itemChanged(Datasource<Commission> ds, @Nullable Commission prevItem, @Nullable Commission item) {
                createMemberBlank.setEnabled(item != null);
            }
        });
    }

    @Override
    protected boolean preCommit() {
        if (ExtConstants.NORM_CONTROL_CODE.equals(typeDs.getItem().getCode())) {
            getItem().setCommission(null);
        } else {
            getItem().setController(null);
        }

        return true;
    }

    public void createMemberBlank() {
        List<CommissionMember> commissionMembers = getItem().getCommission().getMembers();
        Map<String, Object> params = new HashMap<>();
        params.put("members", commissionMembers);
        params.put("defenseDay", getItem());

        openWindow("ext$CreateBlankFrame", WindowManager.OpenType.DIALOG, params);
    }

    public void createTotalBlank() {
        List<DefenseDayStudentLink> links = getItem().getLinks();

        CommissionTotalBlank blank = metadata.create(CommissionTotalBlank.class);
        blank.setDefenseDay(getItem());

        List<TotalMark> marks = new ArrayList<>(links.size());
        for (DefenseDayStudentLink link : links) {
            TotalMark mark = metadata.create(TotalMark.class);
            mark.setBlank(blank);
            mark.setDiplomaStudent(link.getDiplomaStudent());
            marks.add(mark);
        }
        blank.setMarks(marks);
        openEditor("ext$CommissionTotalBlank.edit", blank, WindowManager.OpenType.NEW_TAB);
    }
}
